package offer.algorithm;


import java.util.Arrays;
import java.util.Comparator;


/**
 * Description：
 * Author: zhangc
 * Date：2017/7/27 12:40
 */
public class ArrayMinNum33 {

    public static void main(String[] args) {
        int[] ints = {1, 11, 112, 122, 1122};
        System.out.println(printCombineMinNum(ints));
    }

    public static String printCombineMinNum(int[] numbers) {
        String[] strings = new String[ numbers.length ];
        for (int i = 0; i < numbers.length; i++) {
            strings[ i ] = String.valueOf(numbers[ i ]);
        }
        Arrays.sort(strings, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                if (o1.length() == o2.length())
                    return o1.compareTo(o2);
                if (o1.length() > o2.length()) {
                    for (int i = 0; i < o2.length(); i++) {
                        if (o1.charAt(i) != o2.charAt(i))
                            return o1.charAt(i) - o2.charAt(i);
                    }
                    for (int i = o2.length(); i < o1.length(); i++) {
                        if (o1.charAt(i) != o2.charAt(o2.length() - 1))
                            return o1.charAt(i) - o2.charAt(o2.length() - 1);
                    }
                } else {
                    for (int i = 0; i < o1.length(); i++) {
                        if (o1.charAt(i) != o2.charAt(i))
                            return o1.charAt(i) - o2.charAt(i);
                    }
                    for (int i = o1.length(); i < o2.length(); i++) {
                        if (o1.charAt(o1.length() - 1) != o2.charAt(i))
                            return o1.charAt(o1.length() - 1) - o2.charAt(i);
                    }
                }
                return 0;
            }
        });
        StringBuilder stringBuilder = new StringBuilder();
        for (String string : strings) {
            stringBuilder.append(string);
        }
        return stringBuilder.toString();
    }
}
